#!/bin/bash
# By tzdtwsj.


check_depends() {
status=1
echo "依赖检查中"
for i in $@; do
if test "$(command -v $i)" = "";then
	echo "找不到命令$i"
	status=0
fi
done
if test "$status" = "0"; then
	echo "请安装完这些依赖后再继续"
	exit 1
fi
}

check_depends curl grep sed rm jq

VERSION=$(curl -L https://api.papermc.io/v2/projects/paper|jq .|grep -B 2 "}"|sed -n -e '1p'|awk '{print $1}'|sed 's/"//g')

if test "$VERSION" = ""; then
	echo "获取最新版本paper失败"
	exit 1
fi

build_id=$(curl -L https://api.papermc.io/v2/projects/paper/versions/$VERSION|jq .|grep -B 1 "]"|sed -n '1p'|awk '{print $1}')

download_url="https://api.papermc.io/v2/projects/paper/versions/$VERSION/builds/$build_id/downloads/paper-${VERSION}-${build_id}.jar"

echo 从官网找到MC版本$VERSION
echo 下载中
curl -L "$download_url" -o server.jar
if test "$?" != "0"; then
	echo "服务端下载失败"
	rm -rf server.jar
	exit 1
fi
echo "服务端下载成功！"
echo "你是否同意minecraft最终用户许可协议(eula)？同意将会自动写入eula.txt"
read -p ":(Y/n)" input
if test "$?" != "0"; then input=y; fi
case "$input" in
n|no) echo "你拒绝了eula" ;;
*) echo "eula=true" > eula.txt;;
esac
